<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="客户名称" prop="accountName">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.accountName"
          placeholder="请输入客户名称"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="contactPhone">
        <el-input
          clearable
          :maxlength="11"
          v-model="queryParams.contactPhone"
          placeholder="请输入手机号"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="交易类型" prop="transType">
        <el-select clearable v-model="queryParams.transType" placeholder="请选择" class="!w-200px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_TRADING_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select
          clearable
          v-model="queryParams.accountType"
          placeholder="请选择"
          class="!w-200px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_ACCOUNT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="datetimerange"
          class="!w-400px"
        />
      </el-form-item>
      <el-form-item label="加/减" prop="floatType">
        <el-select clearable v-model="queryParams.floatType" placeholder="请选择" class="!w-200px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_FLOAT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['bid:financeDetail:exportExcel']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60" fixed="left">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="客户名称"
        align="center"
        prop="accountName"
        min-width="200"
        fixed="left"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="账户类型" align="center" prop="accountType" width="200">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_ACCOUNT_TYPE" :value="row.accountType" />
        </template>
      </el-table-column>
      <el-table-column label="加/减" align="center" prop="floatType" width="150">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_FLOAT_TYPE" :value="row.floatType" />
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="center" prop="transType" width="200">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_TRADING_TYPE" :value="row.transType" />
        </template>
      </el-table-column>
      <el-table-column label="交易前余额" align="center" prop="frontTransAmount" width="200" />
      <el-table-column label="交易金额" align="center" prop="transAmount" width="150" />
      <el-table-column label="交易后余额" align="center" prop="backTransAmount" width="200" />
      <el-table-column label="手机号" align="center" prop="contactPhone" width="200" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="250"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <div class="flex justify-between">
      <div class="text-lg mt-3 font-semibold">交易金额合计： {{ totalNum }}万元 </div>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </ContentWrap>
</template>
<script lang="ts" setup name="transDetails">
import * as Utils from '@/utils/dict'
import download from '@/utils/download'
import { useRoute, useRouter } from 'vue-router'
import * as financial from '@/api/financial'
import { dateFormatter } from '@/utils/formatTime'
const total = ref(0)
const list = ref([])
const totalNum = ref(0)
const route = useRoute()
const router = useRouter()
const loading = ref(true)
const queryFormRef = ref()
const message = useMessage()
const exportLoading = ref(false)
const { DICT_TYPE, getIntDictOptions } = reactive(Utils)
const queryParams: Record<string, any> = ref({
  pageNo: 1,
  pageSize: 10,
  accountName: undefined,
  contactPhone: undefined,
  transType: undefined,
  floatType: undefined,
  accountType: undefined,
  createTime: []
})

const handleQuery = () => {
  queryParams.value.pageNo = 1
  getList()
}

const resetQuery = () => {
  router.push({ path: route.path, query: {} })
  queryFormRef.value.resetFields()
  handleQuery()
}

const getList = async () => {
  loading.value = true
  try {
    const data = await financial.getFinanceList(queryParams.value)
    list.value = data.list
    total.value = data.total
    totalNum.value = data.transAmountTotal || 0
  } finally {
    loading.value = false
  }
}

const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await financial.exportFinanceDetailExcel(queryParams.value)
    download.excel(data, '交易明细列表.xls')
  } finally {
    exportLoading.value = false
  }
}

onMounted(async () => {
  if (route.query.accountName) {
    queryParams.value.accountName = route.query.accountName
  }
  getList()
})
</script>
